package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:wujing
 * @Date:2022/12/14 11:53
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    //登录功能
    @PostMapping("/login")
    public R<Employee> checkLogin(@RequestBody Employee emp, HttpServletRequest request) {

        log.info("登录校验,用户信息：{}", emp);

        //实现登录的业务
        // 1、将页面提交的密码password进行md5加密处理
        String password = emp.getPassword();
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());

        // 2、根据页面提交的用户名username查询数据库
        LambdaUpdateWrapper<Employee> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Employee::getUsername, emp.getUsername());
        Employee queryEmp = employeeService.getOne(wrapper);

        // 3、如果没有查询到则返回登录失败结果
        if (queryEmp == null) {
            return R.error("用户名不存在！");
        }

        // /4、密码比对，如果不一致则返回登录失败结果
        if (!queryEmp.getPassword().equals(pwd)) {
            return R.error("密码输入错误，登录失败！");
        }

        // 5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (queryEmp.getStatus() == 0) {
            return R.error("账号已被禁用！");
        }

        // 6、登录成功，将员工id存入Session并返回登录成功结果
        //注意，这里的session名称需要和后面拦截器中session时的名称相同
        request.getSession().setAttribute("emp", queryEmp.getId());
        return R.success(queryEmp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前员工的id
        request.getSession().removeAttribute("emp");
        return R.success("退出成功");
    }

    //查询所有，仅用于测试
    @GetMapping("/queryAll")
    public List<Employee> queryAll() {
        List<Employee> list = employeeService.list();
        return list;
    }

    //分页查询功能
    @GetMapping("/page")
    public R<Page> pageEmp(String page, String pageSize, String name) {

        log.info("page employee:{},{},{}", page, pageSize, name);

        //构建条件构建器和分页对象
        Page<Employee> pageCondition = new Page<>(Integer.parseInt(page), Integer.parseInt(pageSize));

        LambdaUpdateWrapper<Employee> wrapper = new LambdaUpdateWrapper<>();
        wrapper.like(name != null && !name.equals(""), Employee::getName, name);

        //2.查询
        employeeService.page(pageCondition, wrapper);

        return R.success(pageCondition);
    }

    //添加功能
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {

        log.info("新增员工，员工信息：{}", employee.toString());

       //设置初始值123456，需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    //根据id查询员工信息
    @GetMapping("/{id}")
    public R<Employee> queryById(@PathVariable Long id) {
        log.info("根据id查询员工信息...");
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息");
    }


    //编辑员工信息功能
    @PutMapping
    public R<String> update(@RequestBody Employee employee) {
        log.info("修改员工信息");

        //3、调用业务层修改员工信息 updateById()
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");

    }






}




















